package com.yan.crowd.controller;

import com.yan.crowd.api.MysqlRemoteService;
import com.yan.crowd.constant.CrowdConstant;
import com.yan.crowd.entity.vo.*;
import com.yan.crowd.util.ResultEntity;
import org.apache.tomcat.jni.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private MysqlRemoteService mysqlRemoteService;

    @RequestMapping("confirm/return/info/{returnId}")
    public String confirmReturnInfo(@PathVariable("returnId") Integer returnId, HttpSession session) {
        ResultEntity<DetailProjectVO> detailProjectVORemote = mysqlRemoteService.getDetailProjectVORemote(returnId);
        String result = detailProjectVORemote.getResult();

        if (ResultEntity.SUCCESS.equals(result)) {
            session.setAttribute("orderProjectVO", detailProjectVORemote.getData());
        }
        return "order-confirm-return";
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String toConfirmPage(@PathVariable("returnCount") Integer returnCount, HttpSession session) {
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute("orderProjectVO", orderProjectVO);

        MemberLoginVo loginMember = (MemberLoginVo) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer id = loginMember.getId();

        ResultEntity<List<AddressVO>> resultAddress = mysqlRemoteService.getAddressListByMemberIdRemote(id);
        String result = resultAddress.getResult();
        if (ResultEntity.SUCCESS.equals(result)) {
            session.setAttribute("address", resultAddress.getData());
        }
        return "order-confirm-order";
    }

    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO, HttpSession session) {
        ResultEntity<String> resultEntity = mysqlRemoteService.saveAddress(addressVO);
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        Integer returnCount = orderProjectVO.getReturnCount();
        return "redirect:http://localhost/order/confirm/order/" + returnCount;
    }

}
